package com.iteaj.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.stream.Stream;

/**
 * create time: 2019/10/20
 *
 * @author iteaj
 * @since 1.0
 */
public abstract class WebUtils {

    public static String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if(CommonUtils.isNotEmpty(cookies)) {
            for(Cookie item : cookies) {
                if(item.getName().equals(name)) {
                    return item.getValue();
                } else {
                    continue;
                }
            }
            return null;
        } else {
            return null;
        }
    }

    public static void addCookie(HttpServletResponse response, String name, String value) {
        addCookie(response, name, value, "/", -1);
    }

    public static void addCookie(HttpServletResponse response
            , String name, String value, String uri, int maxAge) {
        addCookie(response, name, value, uri, maxAge, false, null, false);
    }

    public static void addCookie(HttpServletResponse response, String name, String value
            , String uri, int maxAge, boolean httpOnly, String domain, boolean secure) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath(uri);
        cookie.setMaxAge(maxAge);
        if(CommonUtils.isNotBlank(domain))
            cookie.setDomain(domain);
        cookie.setSecure(secure);
        cookie.setHttpOnly(httpOnly);
        response.addCookie(cookie);
    }

    /**
     * 获取Http请求的`user-agent`请求头
     * @param request
     * @return
     */
    public static String getUserAgent(HttpServletRequest request) {
        return request.getHeader("user-agent");
    }

    /**
     * user_agent头是否包含某个字符串
     * @param request
     * @param agentStr
     * @return
     */
    public static boolean containAgent(HttpServletRequest request, String agentStr) {
        return getUserAgent(request).contains(agentStr);
    }

    /**
     * 返回客户端类型
     * @param request
     * @return
     */
    public static UserAgent getAgent(HttpServletRequest request) {
        if(isAlipay(request)) return UserAgent.Alipay;
        else if(isWechat(request)) return UserAgent.Wechat;
        else return UserAgent.Other;
    }

    /**
     * 是否是支付宝客户端
     * @param request
     * @return
     */
    public static boolean isAlipay(HttpServletRequest request) {
        return containAgent(request, "Alipay");
    }

    /**
     * 是否是微信客户端
     * @param request
     * @return
     */
    public static boolean isWechat(HttpServletRequest request) {
        return containAgent(request, "MicroMessenger");
    }

    public static void sendRedirect(HttpServletResponse response, String url) {
        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            throw new RuntimeException("发送重定向失败", e);
        }
    }
}
